package com.thinkin.jdbc.dao.impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thinkin.jdbc.dao.BaseDao;
import com.thinkin.jdbc.util.BaseDaoUtil;
import com.thinkin.jdbc.util.DataUtil;
import com.thinkin.jdbc.util.RandomGUID;
import com.thinkin.jdbc.util.RandomUUID;

/**
 * 
 * @author cyt
 * 
 * @param <T>
 */
public class BaseDaoImpl<T> implements BaseDao<T> {
	private Class<?> css;
	BaseDaoUtil baseDaoUtil = null;

	public BaseDaoImpl() {
		super();
		// TODO Auto-generated constructor stub
	}

	public BaseDaoImpl(Class<?> clazz) {
		this.css = clazz;
		baseDaoUtil = new BaseDaoUtil();
		// TODO Auto-generated constructor stub
	}

	public String add(String sql, Object[] param, Boolean blean) {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {

		}
		return result + "";
	}

	public String modify(String sql, Object[] param, Boolean blean) {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {
		}
		return result + "";
	}

	public String delete(String sql, Object[] param, Boolean blean) {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			baseDaoUtil.beginTransaction();
			result = baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {

		}
		return result + "";
	}

	/**
	 * 根据条件删除数据
	 */
	public void deleteList(String sql, String[] param, Boolean blean) {
		// TODO Auto-generated method stub
		try {
			baseDaoUtil.beginTransaction();
			baseDaoUtil.exeSql(sql, param);
			baseDaoUtil.commitTransaction();
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {

		}
	}

	public void deleteListByPk(String sql, String[] pks, Boolean blean) {
		try{
		baseDaoUtil.beginTransaction();
		for (int pk = 0; pks != null && pks.length > 0 && pk < pks.length; pk++) {
			baseDaoUtil.exeSql(sql, (Object) pk);
		}
		baseDaoUtil.commitTransaction();
		if (blean) {
			baseDaoUtil.closeAll();
		}
		}
		catch(Exception e){
			
		}
	}

	/**
	 * 删除所有数据
	 */
	public void deleteAll(String sql, Boolean blean) {
		// TODO Auto-generated method stub
		baseDaoUtil.beginTransaction();
		baseDaoUtil.exeSql(sql);
		baseDaoUtil.commitTransaction();
		if (blean) {
			baseDaoUtil.closeAll();
		}
	}

	public T find(String sql, String pk, Boolean blean) {
		// TODO Auto-generated method stub

		baseDaoUtil.beginTransaction();
		ResultSet result = baseDaoUtil.getRs(sql, pk);
		Object obj = DataUtil.getData(result, css);
		baseDaoUtil.commitTransaction();
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return (T) obj;
	}

	public List<T> findQuery(String sql, Object[] param, Boolean blean) {
		// TODO Auto-generated method stub
		baseDaoUtil.beginTransaction();
		ResultSet result = baseDaoUtil.getRs(sql, param);
		Collection<T> collection = new ArrayList<T>();
		collection = DataUtil.getDatas(collection, result, css);
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return (List<T>) collection;
	}

	public List<T> findQuery(String sql, Boolean blean) {
		// TODO Auto-generated method stub
		Collection<T> collection = new ArrayList<T>();
		try {
			baseDaoUtil.beginTransaction();
			ResultSet result = baseDaoUtil.getRs(sql);
			collection = DataUtil.getDatas(collection, result, css);
			if (blean) {
				baseDaoUtil.closeAll();
			}
		} catch (Exception e) {

		}
		return (List<T>) collection;
	}

	public List<Map<String, Object>> doQuery(String sql, Boolean blean) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			baseDaoUtil.beginTransaction();
			ResultSet rs = baseDaoUtil.getRs(sql);

			if (rs != null) {
				try {
					while (rs.next()) {
						ResultSetMetaData rsmd = rs.getMetaData();
						int colCount = rsmd.getColumnCount();
						Map<String, Object> map = new HashMap<String, Object>();
						for (int i = 0; i < colCount; i++) {
							String key = rsmd.getColumnName(i + 1)
									.toLowerCase();
							Object value = rs.getString(i + 1);
							map.put(key, value);

						}
						list.add(map);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			// TODO 获取执行sql时异常信息，可返回
		}
		if (blean) {
			baseDaoUtil.closeAll();
		}
		return list;
	}

	/**
	 * 
	 * @return
	 */
	public String getGUID() {
		return new RandomGUID().toString();
	}

	/**
	 * 
	 * @return
	 */
	public String getUUID() {
		return new RandomUUID().getUUID();
	}
}
